package com.example.hssmgs.Teacher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.hssmgs.R;
import com.example.hssmgs.Student.CommonDatabase;
import com.example.hssmgs.Student.show_info;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Teacher_look_msg extends Activity {


    SQLiteDatabase db;
    Button go_back;
    Button clean;
    //用于显示信息的列表listview
    TextView tv;


    @SuppressLint("Range")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teacher_look_msg);

        db = new CommonDatabase().getSqliteObject(Teacher_look_msg.this,"test2.db");

        String teacher_id = getIntent().getStringExtra("teacher_id");
        String left_msg = "";
        System.out.println(teacher_id);
        tv=findViewById(R.id.msg);

        @SuppressLint("Recycle") Cursor cursor_look = db.query("message", null, "teacher_id = ?", new String[]{teacher_id}, null, null, null);

        if (cursor_look.getCount() == 0) {
            Toast.makeText(Teacher_look_msg.this, "还没有任何留言", Toast.LENGTH_SHORT).show();
        } else {
            while(cursor_look.moveToNext())
            {
//                将通过id查询到的学生信息显示到界面中
//                tv.setText(cursor_look.getString(cursor_look.getColumnIndex("student_id"+"message")));
                left_msg += cursor_look.getString(cursor_look.getColumnIndex("student_id"));
                left_msg += ":"+"\n";
                left_msg += cursor_look.getString(cursor_look.getColumnIndex("message"));
                left_msg += "\n";

//                System.out.println(left_msg);
            }
            tv.setText(left_msg);
//            tv.postInvalidate();

        }

        clean=findViewById(R.id.clean_msg);
        clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.execSQL("delete from message");
                tv.setText("无");
                tv.postInvalidate();
                Toast.makeText(Teacher_look_msg.this, "留言清空", Toast.LENGTH_SHORT).show();
            }
        });

        go_back=findViewById(R.id.go_back_t);
        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

}
